package com.example.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.example.core.TypeEnum;
import dm.jdbc.driver.DmdbDataSource;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 自定义达梦数据源
 *
 * @author: guohh
 **/
@Data
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceConfig {

    /**
     * 数据源
     */
    private String url;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 驱动名称
     */
    private String driverClassName;

    @Bean
    public DataSource getDataSource() {
        //这边需要sqlServer、PostgreSQL自己定义，正常国内使用mysql和oracle多，个人政务系统国产化会使用DM达梦数据库
        if (driverClassName.contains(TypeEnum.ORACLE.getType()) || driverClassName.contains(TypeEnum.MYSQL.getType())) {
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driverClassName);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            return dataSource;
        } else if (driverClassName.contains(TypeEnum.DM.getType())) {
            DmdbDataSource dmdbDataSource = new DmdbDataSource();
            dmdbDataSource.setURL(getUrl());
            dmdbDataSource.setPassword(getPassword());
            dmdbDataSource.setUser(getUsername());
            return dmdbDataSource;
        }
        return null;
    }
}
